package com.meyoung.day3;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LoginTest {
    WebDriver driver;
    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\cll520\\IdeaProjects\\selenium11\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
        driver.get("https://mail.163.com/");
    }
    @Test
    public void LoginSucess(){
//        //定位iframe
//        WebElement frame = driver.findElement(By.xpath("//*[@id=\"loginDiv\"]/iframe"));
//        //将driver控制权转交给iframe
//        driver.switchTo().frame(frame);
//        //定位账号输入框，并输入账号
//        driver.findElement(By.name("email")).sendKeys("a969117645");
//        //定位密码输入框，并输入密码
//        driver.findElement(By.name("password")).sendKeys("cll930304");
//        //定位登录按钮，点击登录
//        driver.findElement(By.id("dologin")).click();
        LoginTest.login(driver,"a969117645","cll930304");
        //显示等待
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"_mail_tabitem_0_133text\"]")));
        //校验登录是否成功
        String text = driver.findElement(By.xpath("//*[@id=\"_mail_tabitem_0_133text\"]")).getText();

        Assert.assertEquals(text,"首页");

    }
    @Test
    public void LoginError() {
//        //定位iframe
//        WebElement frame = driver.findElement(By.xpath("//*[@id=\"loginDiv\"]/iframe"));
//        //将driver控制权转交给iframe
//        driver.switchTo().frame(frame);
//        //定位账号输入框，并输入账号
//        driver.findElement(By.name("email")).sendKeys("a969117645");
//        //定位密码输入框，并输入密码
//        driver.findElement(By.name("password")).sendKeys("cll9303045");
//        //定位登录按钮，点击登录
//        driver.findElement(By.id("dologin")).click();
        LoginTest.login(driver,"a96965455","1556655");
        //显示等待
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"nerror\"]/div[2]")));

        String error = driver.findElement(By.xpath("//*[@id=\"nerror\"]/div[2]")).getText();
        Assert.assertEquals(error,"帐号或密码错误");
    }
    public static void login(WebDriver driver,String email,String pwd){
        //定位iframe
        WebElement frame = driver.findElement(By.xpath("//*[@id=\"loginDiv\"]/iframe"));
        //将driver控制权转交给iframe
        driver.switchTo().frame(frame);
        //定位账号输入框，并输入账号
        driver.findElement(By.name("email")).sendKeys(email);
        //定位密码输入框，并输入密码
        driver.findElement(By.name("password")).sendKeys(pwd);
        //定位登录按钮，点击登录
        driver.findElement(By.id("dologin")).click();

    }
    @AfterMethod
    public void quiteCase(){
        driver.quit();
    }
}
